package com.iwomy.secureplat.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.iwomy.secureplat.admin.api.feign.RemoteDeptService;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.data.datascope.DataScope;
import com.iwomy.secureplat.common.log.annotation.ReckonTime;
import com.iwomy.secureplat.common.security.util.SecurityUtils;
import com.iwomy.secureplat.platform.constant.RedisConstants;
import com.iwomy.secureplat.platform.constant.RedisExpirationTime;
import com.iwomy.secureplat.platform.dto.BasicInstallLocationDTO;
import com.iwomy.secureplat.platform.entity.*;
import com.iwomy.secureplat.platform.exception.PlatformException;
import com.iwomy.secureplat.platform.mapper.BasicInstallLocationMapper;
import com.iwomy.secureplat.platform.mapper.BasicPlaceBuildingMapper;
import com.iwomy.secureplat.platform.mapper.BasicPlaceFloorMapper;
import com.iwomy.secureplat.platform.service.BasicAlarmContactsService;
import com.iwomy.secureplat.platform.service.BasicDeviceService;
import com.iwomy.secureplat.platform.service.BasicInstallLocationService;
import com.iwomy.secureplat.platform.service.BasicInventoryListService;
import com.iwomy.secureplat.platform.utils.RedisClient;
import com.iwomy.secureplat.platform.vo.BasicDeviceVO;
import com.iwomy.secureplat.platform.vo.BasicInstallLocationVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 安装点位表
 *
 * @author pig
 * @date 2023-11-21 15:12:39
 */
@Service
public class BasicInstallLocationServiceImpl extends ServiceImpl<BasicInstallLocationMapper, BasicInstallLocationEntity> implements BasicInstallLocationService {

	@Autowired
	private BasicDeviceService basicDeviceService;

	@Autowired
	private BasicPlaceBuildingMapper basicPlaceBuildingMapper;

	@Autowired
	private BasicPlaceFloorMapper basicPlaceFloorMapper;

	@Autowired
	private RemoteDeptService remoteDeptService;

	@Autowired
	private BasicAlarmContactsService alarmContactsService;

	@Autowired
	private BasicInventoryListService inventoryListService;

	@Resource
	private RedisClient redisClient;

	@Override
	public Page<BasicInstallLocationVO> getBasicInstallLocationPage(Page page, BasicInstallLocationDTO basicInstallLocationDTO) {

		LambdaQueryWrapper<BasicInstallLocationEntity> queryWrapper = Wrappers.lambdaQuery();
		List<BasicInstallLocationEntity> list = this.baseMapper.selectListByScope(queryWrapper, new DataScope());
		List<Long> installLocationId = list.stream().map(BasicInstallLocationEntity::getId).collect(Collectors.toList());

		if (ObjectUtil.isEmpty(installLocationId)) return page;

		MPJLambdaWrapper<BasicInstallLocationEntity> wrapper = createBaseWrapper();

		if (ArrayUtil.isNotEmpty(basicInstallLocationDTO.getCreateTime())) {
			wrapper.ge(BasicInstallLocationEntity::getCreateTime, basicInstallLocationDTO.getCreateTime()[0])
					.le(BasicInstallLocationEntity::getCreateTime, basicInstallLocationDTO.getCreateTime()[1]);
		}
		if (ObjectUtil.isNotEmpty(basicInstallLocationDTO.getDeptId())) {
			wrapper.in(BasicDeviceEntity::getDeptId, basicInstallLocationDTO.getDeptId());
		}
		if (StrUtil.isNotEmpty(basicInstallLocationDTO.getInstallWorker())) {
			wrapper.like(BasicInstallLocationEntity::getInstallWorkerPhone, basicInstallLocationDTO.getInstallWorker());
		}
		if (ObjectUtil.isNotEmpty(basicInstallLocationDTO.getBuildingId())) {
			wrapper.in(BasicInstallLocationEntity::getBuildingId, basicInstallLocationDTO.getBuildingId());
		}
		if (ObjectUtil.isNotEmpty(basicInstallLocationDTO.getFloorId())) {
			wrapper.in(BasicInstallLocationEntity::getFloorId, basicInstallLocationDTO.getFloorId());
		}
		if (ObjectUtil.isNotEmpty(basicInstallLocationDTO.getAlarmWorkerId())) {
			wrapper.in(BasicInstallLocationEntity::getAlarmWorkerId, basicInstallLocationDTO.getAlarmWorkerId());
		}

		if (StrUtil.isNotEmpty(basicInstallLocationDTO.getImei())) {
			wrapper.like(BasicDeviceEntity::getImei, basicInstallLocationDTO.getImei());
		}

		if (ObjectUtil.isNotEmpty(basicInstallLocationDTO.getDeptLimit())) {
			wrapper.eq(BasicInstallLocationEntity::getDeptId, SecurityUtils.getUser().getDeptId());
		}

		if (StrUtil.isNotEmpty(basicInstallLocationDTO.getAddress())) {
			wrapper.like(BasicInstallLocationEntity::getAddress, basicInstallLocationDTO.getAddress());
		}

		wrapper.in(BasicInstallLocationEntity::getId, installLocationId);

		Page<BasicInstallLocationVO> basicInstallLocation = this.getBaseMapper().selectJoinPage(page, BasicInstallLocationVO.class, wrapper);
		List<BasicInstallLocationVO> records = basicInstallLocation.getRecords();
		records.forEach(this::enrichBasicInstallLocationVO);

		return basicInstallLocation;

	}

	@Override
	public BasicInstallLocationVO getByIds(Long id) {
		MPJLambdaWrapper<BasicInstallLocationEntity> wrapper = createBaseWrapper().eq(BasicInstallLocationEntity::getId, id);
		BasicInstallLocationVO basicInstallLocation = this.getBaseMapper().selectJoinOne(BasicInstallLocationVO.class, wrapper);
		enrichBasicInstallLocationVO(basicInstallLocation);

		return basicInstallLocation;
	}

	@Override
	public Page<BasicInstallLocationVO> getAlarmInstallList(Page page, Long id) {
		MPJLambdaWrapper<BasicInstallLocationEntity> wrapper = new MPJLambdaWrapper<>();
		wrapper.eq(BasicInstallLocationEntity::getAlarmWorkerId, id);
		Page<BasicInstallLocationVO> basicInstallLocation = this.getBaseMapper().selectJoinPage(page, BasicInstallLocationVO.class, wrapper);
		List<BasicInstallLocationVO> records = basicInstallLocation.getRecords();
		records.forEach(this::enrichBasicInstallLocationVO);
		return basicInstallLocation;
	}

	@Override
	public Page<BasicInstallLocationVO> getFloorInstallList(Page page, Long id) {
		MPJLambdaWrapper<BasicInstallLocationEntity> wrapper = createBaseWrapper().eq(BasicInstallLocationEntity::getFloorId, id);

		Page<BasicInstallLocationVO> basicInstallLocation = this.getBaseMapper().selectJoinPage(page, BasicInstallLocationVO.class, wrapper);
		List<BasicInstallLocationVO> records = basicInstallLocation.getRecords();
		records.forEach(this::enrichBasicInstallLocationVO);

		return basicInstallLocation;
	}

	@Override
	public BasicInstallLocationVO getScreen() {

		//大屏安装设备数
		LambdaQueryWrapper<BasicInstallLocationEntity> queryWrapper = Wrappers.lambdaQuery();
		List<BasicInstallLocationEntity> list = this.baseMapper.selectListByScope(queryWrapper, new DataScope());
		List<Long> installLocationId = list.stream().map(BasicInstallLocationEntity::getId).collect(Collectors.toList());
		long placeRoomCount = 0;
		if (ObjectUtil.isNotEmpty(installLocationId)) {
			MPJLambdaWrapper<BasicInstallLocationEntity> wrapper = createBaseWrapper();
			wrapper.in(BasicInstallLocationEntity::getId, installLocationId);

			List<BasicInstallLocationEntity> installLocationEntities = this.baseMapper.selectList(wrapper);
			System.out.println("installLocationEntities=======================" + installLocationEntities);

			if (ObjectUtil.isNotEmpty(installLocationEntities)) {
				placeRoomCount = installLocationEntities.size();
			}
		}
		System.out.println("placeRoomCount安装设备数=======================" + placeRoomCount);


		//大屏设备在线数
		long deviceOnlineCount = 0;
		if (null != basicDeviceService.getDeviceOnlineCount()) {
			deviceOnlineCount = basicDeviceService.getDeviceOnlineCount();
		}
		System.out.println("deviceOnlineCount在线设备数====================" + deviceOnlineCount);

		//大屏设备离线数
		long deviceOfflineCount = 0;
		if (null != basicDeviceService.getDeviceOfflineCount()) {
			deviceOfflineCount = basicDeviceService.getDeviceOfflineCount();
		}
		System.out.println("deviceOfflineCount在线设备数====================" + deviceOfflineCount);

		//大屏下级设备数
		Long deptId = SecurityUtils.getUser().getDeptId();
		long lowerDeptCount = remoteDeptService.getDescendantList(deptId).getData().size() - 1;
		System.out.println("lowerDeptCount下级设备====================" + lowerDeptCount);

		//大屏正常设备数
		long deviceNormalCount = 0;
		if (null != basicDeviceService.getDeviceNormalCount()) {
			deviceNormalCount = basicDeviceService.getDeviceNormalCount();
		}
		System.out.println("deviceNormalCount正常设备数====================" + deviceNormalCount);

		//大屏故障设备数
		long deviceFaultCount = 0;
		if (null != basicDeviceService.getDeviceFaultCount()) {
			deviceFaultCount = basicDeviceService.getDeviceFaultCount();
		}
		System.out.println("deviceFaultCount故障设备数====================" + deviceFaultCount);


		BasicInstallLocationVO basicInstallLocationVO = new BasicInstallLocationVO();
		basicInstallLocationVO.setPlaceRoomCount(placeRoomCount);
		basicInstallLocationVO.setDeviceOnlineCount(deviceOnlineCount);
		basicInstallLocationVO.setDeviceOfflineCount(deviceOfflineCount);
		basicInstallLocationVO.setLowerDeptCount(lowerDeptCount);
		basicInstallLocationVO.setDeviceNormalCount(deviceNormalCount);
		basicInstallLocationVO.setDeviceFaultCount(deviceFaultCount);
		return basicInstallLocationVO;
	}

	@Override
	public List<BasicInstallLocationEntity> listByScope(LambdaQueryWrapper<BasicInstallLocationEntity> queryWrapper, DataScope dataScope) {
		List<BasicInstallLocationEntity> list = this.baseMapper.selectListByScope(queryWrapper, new DataScope());
		return list;
	}

	@Override
	public R newlyRemoveById(BasicInstallLocationDTO basicInstallLocationDTO) {
		if (basicInstallLocationDTO.getReturnDept() == 20) {
			returnDept20(basicInstallLocationDTO);
		} else {
			returnDept10(basicInstallLocationDTO);
		}
		return R.ok(this.removeBatchByIds(CollUtil.toList(basicInstallLocationDTO.getIds())));
	}

	//安装点变更imei号
	@Override
	public R replaceImei(BasicInstallLocationDTO basicInstallLocationDTO) {
		//安装点绑定新设备
		BasicDeviceEntity newDeviceEntity = new BasicDeviceEntity();
		newDeviceEntity.setImei(basicInstallLocationDTO.getImei());
		LambdaQueryWrapper<BasicDeviceEntity> newWrapper = Wrappers.lambdaQuery(newDeviceEntity);
		BasicDeviceEntity newDevice = basicDeviceService.getOne(newWrapper);
		if (newDevice.getStocks() == 10) {
			throw new PlatformException("该设备未出库，无法安装绑定或过户，请联系设备供应商。");
		}

		//原本的设备接触绑定
		BasicDeviceEntity wrapperEntity = new BasicDeviceEntity();
		wrapperEntity.setInstallLocationId(basicInstallLocationDTO.getId());
		LambdaQueryWrapper<BasicDeviceEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicDeviceEntity deviceEntity = basicDeviceService.getOne(wrapper);
		if (deviceEntity != null) {
			deviceEntity.setInstallLocationId(0L);
			deviceEntity.setBindState(20);
			basicDeviceService.updateById(deviceEntity);
		}

		//安装点修改设备id和设备归属单位
		LambdaQueryWrapper<BasicInstallLocationEntity> wrappers = Wrappers.lambdaQuery();
		wrappers.eq(BasicInstallLocationEntity::getId, basicInstallLocationDTO.getId());
		BasicInstallLocationEntity installLocation = this.getOne(wrappers);
		if (installLocation != null) {
			installLocation.setDeptId(basicInstallLocationDTO.getDeptId());
			installLocation.setDeviceId(basicInstallLocationDTO.getDeviceId());
			this.getBaseMapper().updateById(installLocation);
		}

		if (newDevice != null) {
			newDevice.setInstallLocationId(basicInstallLocationDTO.getId());
			newDevice.setBindState(30);
			basicDeviceService.updateById(newDevice);
		}

		return R.ok();
	}

	@Override
	public R newlyAdded(BasicInstallLocationEntity basicInstallLocation) {
		Long deviceId = basicInstallLocation.getDeviceId();
		BasicDeviceEntity wrapperEntity = new BasicDeviceEntity();
		wrapperEntity.setId(deviceId);
		LambdaQueryWrapper<BasicDeviceEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicDeviceEntity one = basicDeviceService.getOne(wrapper);
		if (one.getStocks() == 10) {
			throw new PlatformException("该设备未出库，无法安装绑定或过户，请联系设备供应商。");
		}
		if (one.getBindState() == 30) {
			throw new PlatformException("该设备已被绑定");
		}
		basicInstallLocation.setInstallWorker(SecurityUtils.getUser().getName());
		basicInstallLocation.setInstallWorkerPhone(SecurityUtils.getUser().getPhone());
		basicInstallLocation.setDeptId(basicInstallLocation.getDeptId());
		this.save(basicInstallLocation);

		if (!one.getDeptId().equals(basicInstallLocation.getDeptId())) {
			BasicInventoryListEntity inventoryListEntity = new BasicInventoryListEntity();
			inventoryListEntity.setDeviceId(deviceId);
			LambdaQueryWrapper<BasicInventoryListEntity> inventoryListWrapper = Wrappers.lambdaQuery(inventoryListEntity);
			BasicInventoryListEntity inventoryListOne = inventoryListService.getOne(inventoryListWrapper);

			inventoryListOne.setTransferDeptId(basicInstallLocation.getDeptId());
			inventoryListService.updateById(inventoryListOne);
		}


		LambdaQueryWrapper<BasicInstallLocationEntity> wrappers = Wrappers.lambdaQuery();
		wrappers.eq(BasicInstallLocationEntity::getDeviceId, deviceId);
		BasicInstallLocationEntity installLocation = this.getOne(wrappers);
		wrapperEntity.setInstallLocationId(installLocation.getId());
		wrapperEntity.setDeptId(basicInstallLocation.getDeptId());
		wrapperEntity.setInstallWorker(SecurityUtils.getUser().getName());
		wrapperEntity.setBindState(30);
		basicDeviceService.updateById(wrapperEntity);
		getInstallLocation(installLocation.getId());
		BasicAlarmContactsEntity alarmContacts = new BasicAlarmContactsEntity();
		alarmContacts.setId(basicInstallLocation.getAlarmWorkerId());
		int installCount = (int) this.count(new QueryWrapper<BasicInstallLocationEntity>()
				.eq("alarm_worker_Id", basicInstallLocation.getAlarmWorkerId()));
		alarmContacts.setInstallCount(installCount);
		alarmContactsService.updateById(alarmContacts);

		return R.ok();
	}

	//批量绑定安装点
	@Override
	public R newlySaveBatch(BasicInstallLocationDTO basicInstallLocationDTO) {
		List<BasicInstallLocationEntity> installLocations = new ArrayList<>();
		List<BasicInventoryListEntity> inventoryList = new ArrayList<>();

		LambdaQueryWrapper<BasicDeviceEntity> wrapper = new LambdaQueryWrapper<>();
		wrapper.in(BasicDeviceEntity::getId, basicInstallLocationDTO.getDeviceIds());
		List<BasicDeviceEntity> deviceEntities = basicDeviceService.list(wrapper);

		for (BasicDeviceEntity deviceEntity : deviceEntities) {
			BasicInstallLocationEntity basicInstallLocation = new BasicInstallLocationEntity();
			BeanUtils.copyProperties(basicInstallLocationDTO, basicInstallLocation);
			basicInstallLocation.setDeviceId(deviceEntity.getId());
			basicInstallLocation.setInstallWorker(SecurityUtils.getUser().getName());
			basicInstallLocation.setInstallWorkerPhone(SecurityUtils.getUser().getPhone());
			basicInstallLocation.setDeptId(basicInstallLocation.getDeptId());
			installLocations.add(basicInstallLocation);

			if (!deviceEntity.getDeptId().equals(basicInstallLocation.getDeptId())) {
				BasicInventoryListEntity inventoryListEntity = new BasicInventoryListEntity();
				inventoryListEntity.setDeviceId(deviceEntity.getId());
				LambdaQueryWrapper<BasicInventoryListEntity> inventoryListWrapper = Wrappers.lambdaQuery(inventoryListEntity);
				BasicInventoryListEntity inventoryListOne = inventoryListService.getOne(inventoryListWrapper);

				inventoryListOne.setTransferDeptId(basicInstallLocation.getDeptId());
				inventoryList.add(inventoryListOne);
			}
		}
		inventoryListService.updateBatchById(inventoryList);
		this.saveBatch(installLocations);

		List<BasicDeviceEntity> basicDeviceEntities = new ArrayList<>();
		LambdaQueryWrapper<BasicInstallLocationEntity> wrappers = Wrappers.lambdaQuery();
		wrappers.in(BasicInstallLocationEntity::getDeviceId, basicInstallLocationDTO.getDeviceIds());
		List<BasicInstallLocationEntity> installLocation = this.list(wrappers);
		for (BasicInstallLocationEntity installLocationEntity : installLocation) {
			BasicDeviceEntity deviceEntity = new BasicDeviceEntity();
			deviceEntity.setId(installLocationEntity.getDeviceId());
			deviceEntity.setInstallLocationId(installLocationEntity.getId());
			deviceEntity.setInstallWorker(SecurityUtils.getUser().getName());
			deviceEntity.setDeptId(basicInstallLocationDTO.getDeptId());
			deviceEntity.setBindState(30);
			basicDeviceEntities.add(deviceEntity);
		}
		basicDeviceService.updateBatchById(basicDeviceEntities);

		for (BasicInstallLocationEntity installLocationEntity : installLocation) {
			getInstallLocation(installLocationEntity.getId());
		}
		BasicAlarmContactsEntity alarmContacts = new BasicAlarmContactsEntity();
		alarmContacts.setId(basicInstallLocationDTO.getAlarmWorkerId());
		int installCount = (int) this.count(new QueryWrapper<BasicInstallLocationEntity>()
				.eq("alarm_worker_Id", basicInstallLocationDTO.getAlarmWorkerId()));
		alarmContacts.setInstallCount(installCount);
		alarmContactsService.updateById(alarmContacts);

		return R.ok();
	}

	@Override
	@ReckonTime
	public BasicInstallLocationVO getInstallLocation(Long id) {
		BasicInstallLocationVO basicInstallLocationVO = new BasicInstallLocationVO();
		String key = RedisConstants.REDIS_KEY_DEVICE_INSTALL_LOCATION + id;
		basicInstallLocationVO = redisClient.get(key, BasicInstallLocationVO.class);

		if (basicInstallLocationVO == null) {
			MPJLambdaWrapper<BasicInstallLocationEntity> wrapper = createBaseWrapper().eq(BasicInstallLocationEntity::getId, id);
			BasicInstallLocationEntity basicInstallLocation = this.getBaseMapper().selectOne(wrapper);
			BasicInstallLocationVO installLocationVO = new BasicInstallLocationVO();
			BeanUtil.copyProperties(basicInstallLocation,installLocationVO);
			redisClient.set(key, JSONObject.toJSONString(installLocationVO), RedisExpirationTime.REDIS_TIME_DEVICE_INSTALL_LOCATION);
			return installLocationVO;
		}

		return basicInstallLocationVO;
	}


	private MPJLambdaWrapper<BasicInstallLocationEntity> createBaseWrapper() {
		return new MPJLambdaWrapper<BasicInstallLocationEntity>()
				.selectAll(BasicInstallLocationEntity.class)
				.selectAs(BasicDeviceEntity::getInstallLocationId, BasicInstallLocationVO::getId)
				.selectAs(BasicDeviceEntity::getId, BasicInstallLocationVO::getDeviceId)
				.selectAs(BasicDeviceEntity::getDeptId, BasicInstallLocationVO::getDeptId)
				.selectAs(BasicDeviceEntity::getDeviceState, BasicInstallLocationVO::getDeviceState)
				.selectAs(BasicAlarmContactsEntity::getName, BasicInstallLocationVO::getAlarmWorkerName)
				.selectAs(BasicAlarmContactsEntity::getPhone, BasicInstallLocationVO::getAlarmWorkerPhone)

				.leftJoin(BasicDeviceEntity.class, BasicDeviceEntity::getInstallLocationId, BasicInstallLocationEntity::getId)
				.leftJoin(BasicAlarmContactsEntity.class, BasicAlarmContactsEntity::getId, BasicInstallLocationEntity::getAlarmWorkerId);

	}

	private void enrichBasicInstallLocationVO(BasicInstallLocationVO record) {
		Long deviceId = record.getDeviceId();
		if (deviceId != null) {
			BasicDeviceVO basicDevice = basicDeviceService.getByIds(deviceId);
			if (basicDevice != null) {
				record.setDeptName(basicDevice.getDeptName());
				record.setDeviceType(basicDevice.getDeviceType());
				record.setDeviceModelName(basicDevice.getDeviceModelName());
				record.setImei(basicDevice.getImei());
			}
		}
		if (record.getBuildingId() != null) {
			LambdaQueryWrapper<BasicPlaceBuildingEntity> wrapper = new LambdaQueryWrapper<>();
			wrapper.eq(BasicPlaceBuildingEntity::getId, record.getBuildingId());
			BasicPlaceBuildingEntity basicPlaceBuilding = basicPlaceBuildingMapper.selectOne(wrapper);
			record.setBuildingName(basicPlaceBuilding.getBuildingName());
		}
		if (record.getFloorId() != null) {
			LambdaQueryWrapper<BasicPlaceFloorEntity> wrapper = new LambdaQueryWrapper<>();
			wrapper.eq(BasicPlaceFloorEntity::getId, record.getFloorId());
			BasicPlaceFloorEntity basicPlaceFloor = basicPlaceFloorMapper.selectOne(wrapper);
			record.setFloor(basicPlaceFloor.getFloorName());
		}
	}

	//解绑安装点设备不返回上一级
	private void returnDept20(BasicInstallLocationDTO basicInstallLocationDTO) {
		LambdaQueryWrapper<BasicDeviceEntity> wrapper = Wrappers.lambdaQuery(BasicDeviceEntity.class)
				.in(BasicDeviceEntity::getInstallLocationId, basicInstallLocationDTO.getIds());
		List<BasicDeviceEntity> devices = basicDeviceService.list(wrapper);
		// 修改设备的绑定状态
		for (BasicDeviceEntity device : devices) {
			device.setBindState(20);
			device.setInstallLocationId(0L);
		}
		// 批量更新设备信息
		boolean isUpdated = basicDeviceService.updateBatchById(devices);
		if (!isUpdated) {
			throw new PlatformException("更新设备信息失败");
		}
	}

	//解绑安装点设备返回上一级
	private void returnDept10(BasicInstallLocationDTO basicInstallLocationDTO) {
		LambdaQueryWrapper<BasicDeviceEntity> wrapper = Wrappers.lambdaQuery(BasicDeviceEntity.class)
				.in(BasicDeviceEntity::getInstallLocationId, basicInstallLocationDTO.getIds());
		List<BasicDeviceEntity> devices = basicDeviceService.list(wrapper);

		List<BasicInventoryListEntity> inventoryListEntityList = new ArrayList<>();
		// 修改设备的绑定状态
		for (BasicDeviceEntity device : devices) {
			Long deviceId = device.getId();
			LambdaQueryWrapper<BasicInventoryListEntity> inventoryListWrapper = new LambdaQueryWrapper<>();
			inventoryListWrapper.eq(BasicInventoryListEntity::getDeviceId, deviceId);
			BasicInventoryListEntity inventoryListEntity = inventoryListService.getOne(inventoryListWrapper);

			//过户人为出库单位时，解绑安装点时将设备返还给出库单位
			if (inventoryListEntity.getCreateBy().equals(inventoryListEntity.getUpdateBy())) {
				inventoryListEntity.setTransferDeptId(inventoryListEntity.getDeptId());
				device.setDeptId(inventoryListEntity.getDeptId());
			} else {
				//过户人为被出库单位时，解绑安装点时将设备返还给被出库单位
				inventoryListEntity.setTransferDeptId(inventoryListEntity.getCustomerDeptId());
				device.setDeptId(inventoryListEntity.getCustomerDeptId());
			}
			device.setBindState(20);
			device.setInstallLocationId(0L);
			inventoryListEntityList.add(inventoryListEntity);
		}
		inventoryListService.updateBatchById(inventoryListEntityList);
		basicDeviceService.updateBatchById(devices);
	}
}